package org.qdog.pk.pkscrape.entity;

import org.apache.log4j.Logger;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

/**
 * @since 08/2011
 */
@javax.persistence.Entity
@Table(name = "relationships")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 20)
public abstract class Relationship implements PersistentObject {
  private static final Logger log = Logger.getLogger(Relationship.class);

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id", nullable = false)
  private Long id;

  @Column(name = "source_entity_id", nullable = false)
  private String sourceEntityId;

  @Column(name = "target_entity_id", nullable = false)
  private String targetEntityId;

  @Temporal(TemporalType.DATE)
  @Column(name = "start_date", nullable = false)
  private Date start;

  @Temporal(TemporalType.DATE)
  @Column(name = "end_date", nullable = true)
  private Date end;

  public Relationship() {

  }

  public Relationship(Entity sourceEntity, Entity targetEntity) {
    this.sourceEntityId = sourceEntity.getId();
    this.targetEntityId = targetEntity.getId();
    this.start = new Date();
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public void setSourceEntity(Entity sourceEntity) {
    this.sourceEntityId = sourceEntity.getId();
  }

  public void setTargetEntity(Entity targetEntity) {
    this.targetEntityId = targetEntity.getId();
  }

  public Date getStart() {
    return start;
  }

  public void setStart(Date start) {
    this.start = start;
  }

  public Date getEnd() {
    return end;
  }

  public void setEnd(Date end) {
    this.end = end;
  }

  public String getSourceEntityId() {
    return sourceEntityId;
  }

  public void setSourceEntityId(String sourceEntityId) {
    this.sourceEntityId = sourceEntityId;
  }

  public String getTargetEntityId() {
    return targetEntityId;
  }

  public void setTargetEntityId(String targetEntityId) {
    this.targetEntityId = targetEntityId;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Relationship)) return false;

    Relationship that = (Relationship) o;

    if (!id.equals(that.id)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return id.hashCode();
  }
}
